
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        goodsAttributeDialogVisible: false,
        stepPriceDialogVisible: false,
		title: null,
		goodsCraftPrice: {},
        goodsCraftAttributeData: [],// 基础商品属性table
        goodsCraftData:[],// 基础商品工艺
        goodsCraftOptionData:[],// 基础商品工艺选项table
        stepPriceData:[],// 阶梯价格table
        stepPriceRow: null, //当前阶梯价所在option 行
	},

	mounted: function () {
		this.initData();
    },

	updated: function () {
        //将已经存在的赋值选中
        for (var i = 0; i < vm.goodsCraftOptionData.length; i++) {
            var temp = vm.goodsCraftOptionData[i];
            if (temp.craftPriceId) {
                this.$refs.goodsCraftOptionTable.toggleRowSelection(temp, true);
            }
        }
    },

	methods: {
		/*页面初始化数据*/
		initData : function () {
			// 请求已经存在的 商品工艺价格
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsCraftPriceList", function(r){
                vm.goodsCraftAttributeData = r.goodsCraftPriceData.goodsCraftList;

                //重新赋值stepPrice
                if (r.goodsCraftPriceData.goodsCraftPriceList){
                    for(var i=0; i<r.goodsCraftPriceData.goodsCraftPriceList.length; i++){
                        var temp =  r.goodsCraftPriceData.goodsCraftPriceList[i];
                        if (temp.stepPrice){
                            temp.stepPrice = JSON.parse(temp.stepPrice);
                        }
                    }
                }
                vm.goodsCraftOptionData = r.goodsCraftPriceData.goodsCraftPriceList;

            });
        },

		// 添加商品工艺
        addGoodsCraft: function () {
        	// 查询商品工艺
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsCraftList", function(r){
                vm.goodsCraftData = r.goodsCraftList;
                vm.goodsAttributeDialogVisible = true;
            });

        },

		// 添加当前行到左侧 基础商品属性表
        addToLeftTable: function (index) {
        	var temp =  vm.goodsCraftData[index];
            var selected =  vm.goodsCraftAttributeData.find(function (option) {
				return option.attributeId === temp.attributeId;
            })
			if (selected){
                this.$message.error('该工艺已经添加 !');
            	return;
			}
            this.$refs.goodsCraftTable.toggleRowSelection(temp,true);
            vm.goodsCraftAttributeData.push(temp);

        },

		// 添加选中的到当前行
        addSelectedToLeftTable: function () {
            if (this.$refs.goodsCraftTable.selection.length == 0){
                this.$message.error('至少选中一个工艺 !');
            	return;
			}

			for(var index in this.$refs.goodsCraftTable.selection){
            	var temp = this.$refs.goodsCraftTable.selection[index];
                var selected =  vm.goodsCraftAttributeData.find(function (option) {
                    return option.attributeId === temp.attributeId;
                })
				if(!selected){
                    vm.goodsCraftAttributeData.push(temp);
				}
			}

			vm.goodsAttributeDialogVisible = false;
        },

		// 工艺当前行 点击事件
        goodsCraftAttributeClicked: function (row, event, column) {
            // 查询商品工艺选项
            $.get(baseURL + "goodsCraftPrice/goodscraftprice/getGoodsCraftOptionList?attributeId="+row.attributeId, function(r){
                //重新赋值stepPrice
                if (r.goodsCraftOptionPriceList){
                    for(var i=0; i<r.goodsCraftOptionPriceList.length; i++){
                        var temp =  r.goodsCraftOptionPriceList[i];
                        if (temp.stepPrice){
                            temp.stepPrice = JSON.parse(temp.stepPrice);
                        }
                    }
                }
                vm.goodsCraftOptionData = r.goodsCraftOptionPriceList;
            });

        },

		/*设置阶梯价格*/
        openStepPriceDialog: function (row) {
            //先初始化数据
			if(row.stepPrice){
                vm.stepPriceData = row.stepPrice;

			}else {
                vm.stepPriceData = [];
                var temp={"beginNum": null, "endNum": null, "singlePrice": null , "remark": null};
                // 加入一条空白数据
                vm.stepPriceData.push(temp);
            }

            // 保留当前option 所在行
            vm.stepPriceRow = row;
        	vm.stepPriceDialogVisible = true;
        },

		/*保存阶梯价*/
        saveStepPrice: function () {
        	//todo 数据校验

			var row = vm.stepPriceRow;

            row.stepPrice = vm.stepPriceData;

            // 清空临时数据
            vm.stepPriceRow = null;
            vm.stepPriceData = [];
            vm.stepPriceDialogVisible = false;
        },

		/*添加一行阶梯价*/
        addOneStepPrice: function () {
            var temp={"beginNum": null, "endNum": null, "singlePrice": null , "remark": null};
            vm.stepPriceData.push(temp);
        },

		/*删除一行数据*/
        deleteOneStepPrice: function (index, rows) {
            if(rows.length >1){
                rows.splice(index, 1);
            }
        },

		/*一键设置价格为第一行价格*/
        oneKeySetPrice: function () {
            this.$confirm('确定要把所有选项价格一键设置为第一行价格吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 取出第一行数据
                for(var i= 1 ; i < vm.goodsCraftOptionData.length ;i++){
                    // 从第二行开始 赋值
                    var temp = vm.goodsCraftOptionData[i];
                    temp.singlePrice = vm.goodsCraftOptionData[0].singlePrice;
                    temp.singleStartingPrice = vm.goodsCraftOptionData[0].singleStartingPrice;
                    temp.startingPrice = vm.goodsCraftOptionData[0].startingPrice;
                    temp.floatCoefficient = vm.goodsCraftOptionData[0].floatCoefficient;
                    temp.stepPrice = vm.goodsCraftOptionData[0].stepPrice;
                    temp.templatePrice = vm.goodsCraftOptionData[0].templatePrice;
                }
                // 并全部勾选
                this.$refs.goodsCraftOptionTable.toggleAllSelection();


            }).catch(() => {
                this.$message({type: 'info', message: '已取消一键设价'});
            })

        },

		/*保存基础商品工艺价格*/
        saveGoodsCraftPrice: function () {
			var postData =[];
			// 先拷贝一个数据 避免页面数据错误
            var newSelectionObject = jQuery.extend(true, [], this.$refs.goodsCraftOptionTable.selection);
            for(var index  in newSelectionObject){
				var currentSelection = newSelectionObject[index];
                currentSelection.stepPrice = JSON.stringify(currentSelection.stepPrice);
                postData.push(currentSelection);
			}

			if(postData.length == 0){
                this.$message.error('至少选中一个工艺选项 !');
                return;
            }
            if (postData.length > 30){
                this.$message.error('一次最多添加30个工艺选项价格 !');
                return;
            }
            var self = this;

            $.ajax({
                type: "POST",
                url: baseURL + "goodsCraftPrice/goodscraftprice/save",
                contentType: "application/json",
                data:JSON.stringify({"craftPriceList": postData}),
                success: function(r){
                    if(r.code === 0){
                        self.$message({message: '保存成功 !' ,type: 'success'});
                    }else{
                        alert(r.msg);
                    }
                }
            });



        },

		/*根据工艺删除价格*/
        deleteCraftAttribute: function (index, rows) {

            var self =this;
        	var row = rows[index];
        	// 判断后台是否 已经存在价格
            if(row.craftPriceId){
				//存在id 则请求后台删除 此处id 仅代表存在价格 并不是该工艺的价格id
                this.$confirm('确定要删除'+ row.attributeName+"价格吗?", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        type: "get",
                        url: baseURL + "goodsCraftPrice/goodscraftprice/deleteByAttributeId?attributeId="+row.attributeId,
                        success: function(r){
                            if(r.code == 0){
                                self.$message({message: '删除成功 !' ,type: 'success'});
                                self.initData();
                                /*rows.splice(index, 1);
                                vm.goodsCraftOptionData = [];*/
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                })
			}else {
				// 仅仅删除页面数据即可
                rows.splice(index, 1);
                vm.goodsCraftOptionData = [];
			}

        },

        /*根据选中工艺价格删除*/
        deleteCraftPrice: function () {
            // 取出所有选中的工艺价格
           var selected =  this.$refs.goodsCraftOptionTable.selection;
           if(selected.length ==0){
               this.$message.error('至少选中一个工艺价格 !');
               return;
           }
           var postIds = [];
           var selectedCraftPriceIds = [];
           for(var index in selected){ //循环选中项
               if(selected[index].craftPriceId){
                   //后台存在真是数据
                   postIds.push(selected[index].craftPriceId);
                   selectedCraftPriceIds.push(selected[index]);
               }else {
                   vm.goodsCraftOptionData.splice($.inArray(selected[index], vm.goodsCraftOptionData), 1)
                   //仅前台页面数据 直接删除即可
               }
           }

            var self = this;
           if(postIds.length > 0){
               // 后台ajax 请求删除
               this.$confirm('确定要删除已配价选项吗?', '提示', {
                   confirmButtonText: '确定',
                   cancelButtonText: '取消',
                   type: 'warning'
               }).then(() => {
                   $.ajax({
                       type: "POST",
                       url: baseURL + "goodsCraftPrice/goodscraftprice/deleteByIds",
                       contentType: "application/json",
                       data: JSON.stringify(postIds),
                       success: function(r){
                           if(r.code == 0){
                               self.$message({message: '删除成功 !' ,type: 'success'});
                               self.initData();
                               // 此处循环删除页面
                             /*  for(var selectedCraftPriceId in selectedCraftPriceIds){
                                   vm.goodsCraftOptionData.splice($.inArray(selectedCraftPriceId, vm.goodsCraftOptionData), 1)
                               }*/
                           }else{
                               alert(r.msg);
                           }
                       }
                   });
               }).catch(() => {
                   this.$message({type: 'info', message: '已取消删除'});
               })
           }

        },


	}
});